package top.lixb.libcommon.net;


import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.vondear.rxtool.RxLogTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialog;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;
import com.vondear.rxui.view.dialog.RxDialogTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.reactivex.Flowable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;
import top.lixb.libcommon.base.BaseApplication;
import top.lixb.libcommon.log.L;

public abstract class CommonCallback implements Observer {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        RxToast.showToast("网络错误:"+e.getMessage());
    }

    private String responseStr;
    @Override
    public void onNext(Object o) {
        if (o instanceof ResponseBody) {
            try {
                responseStr = ((ResponseBody) o).string();
                if (!TextUtils.isEmpty(responseStr) && responseStr.contains(",\"data\":[]")) {
                    responseStr = responseStr.replace(",\"data\":[]", "");
                }

                String errordata = "\"data\":\"\",";
                if (!TextUtils.isEmpty(responseStr) && responseStr.contains(errordata)) {
                    responseStr = responseStr.replace(errordata, "");
                }

                L.e("CZF::" + responseStr);
                onRecv(responseStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



    public abstract void onRecv(String response);
}
